package lib.control;

import java.util.ArrayList;
import java.util.List;

import lib.JTheTVDBAdapter;
import lib.api.thetvdb.model.TheTVDBEpisode;
import lib.exception.VideoControlException;
import lib.logging.Level;
import lib.logging.Logger;
import lib.model.EpisodeFile;
import lib.model.MovieFile;
import lib.model.Subtitle;
import settings.model.Settings;

public class NameSearchControl {

	private final JTheTVDBAdapter jTheTVDBAdapter;
	private final Settings settings;
	private final SubtitleControl subtitleControl;

	public NameSearchControl(Settings settings) {
		subtitleControl = new SubtitleControl(settings);
		jTheTVDBAdapter = new JTheTVDBAdapter();
		this.settings = settings;
	}

	public List<Subtitle> SearchSubtitles(String movie, String languagecode) {
		MovieFile movieFile = new MovieFile();
		movieFile.setTitle(movie);
		return subtitleControl.getSubtitles(movieFile, languagecode);
	}

	@SuppressWarnings("serial")
	public List<Subtitle> SearchSubtitles(String serie, int season,
			final int episode, String languagecode) throws Exception {
		List<Subtitle> subs = new ArrayList<Subtitle>();
		EpisodeFile ep = new EpisodeFile();
		ep.setShow(serie.replace("(", "").replace(")", "")); // clean up for the
																// year in show
																// title
		if (season > 0) {
			ep.setSeason(season);
			if (episode > 0) {
				ep.setEpisodeNumbers(new ArrayList<Integer>() {
					{
						add(episode);
					}
				});
				subs.addAll(searchSubtitle(ep, languagecode));
			} else {
				List<TheTVDBEpisode> episodes = jTheTVDBAdapter.getAllEpisodes(
						ep.getTvdbid(), languagecode);
				for (TheTVDBEpisode tvdbEpisode : episodes) {
					if (tvdbEpisode.getSeasonNumber() == season) {
						subs.addAll(setTVDBAndSearchSubtitle(tvdbEpisode, ep,
								languagecode));
					}
				}
			}
		} else {
			List<TheTVDBEpisode> episodes = jTheTVDBAdapter.getAllEpisodes(
					ep.getTvdbid(), languagecode);
			for (TheTVDBEpisode tvdbEpisode : episodes) {
				subs.addAll(setTVDBAndSearchSubtitle(tvdbEpisode, ep,
						languagecode));
			}
		}
		return subs;
	}

	private List<Subtitle> setTVDBAndSearchSubtitle(TheTVDBEpisode tvdbEpisode,
			EpisodeFile ep, String languagecode) {
		List<Subtitle> subs = new ArrayList<Subtitle>();

		ep.setSeason(tvdbEpisode.getSeasonNumber());
		List<Integer> episodenumbers = new ArrayList<Integer>();
		episodenumbers.add(tvdbEpisode.getEpisodeNumber());
		ep.setEpisodeNumbers(episodenumbers);

		EpisodeFileControl epfc = new EpisodeFileControl(ep, settings);

		try {
			subs.addAll(searchSubtitle(epfc.process(settings.getMappingList()),
					languagecode));
		} catch (VideoControlException e) {
			if (Logger.instance.getLogLevel().intValue() < Level.INFO
					.intValue()) {
				Logger.instance.error(Logger.stack2String(e));
			} else {
				Logger.instance.error(e.getMessage());
			}
		}

		return subs;
	}

	private List<Subtitle> searchSubtitle(EpisodeFile ep, String languagecode) {
		return subtitleControl.getSubtitles(ep, languagecode);
	}
}
